package com.woniu.p2p.controller;

import com.woniu.p2p.entity.*;
import com.woniu.p2p.service.BorrowService;
import com.woniu.p2p.util.FutureDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * BorrowController
 *
 *
 */

@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    BorrowService borrowService;

    /**
     * 查询所有贷款用途
     * @return
     */
    @RequestMapping("/findAllLoansUsemethod")
    @ResponseBody
   public List<LoansUsemethod> findAllLoansUsemethod(){
        return borrowService.findAllLoansUsemethod();
    }

    /**
     * 查询所有还款方式
     * @return
     */
    @RequestMapping("/findAllInvestRepay")
    @ResponseBody
    public List<InvestRepay> findAllInvestRepay(){
        return  borrowService.findAllInvestRepay();
    }

    /**
     * 增加贷款记录以及个人中心记录
     * @param loans
     * @param request
     * @return
     */
    @RequestMapping("/addLoans")
    @ResponseBody
    public int AddLoans(@RequestBody Loans loans, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        loans.setLoansTime(new Date());
        loans.setUserid(userid);
        loans.setLoansStatus(2);
        UserZong userZong=new UserZong();
        userZong.setUserid(userid);
        userZong.setZongPaytime(new Date());
        userZong.setZongPaymoney(loans.getLoansMoney());
        userZong.setZongPaytype("借贷");
        userZong.setZongPayservice(new Double(loans.getLoansMoney()*10000*0.003).longValue());

//        UserLoans userLoans=new UserLoans();
//        userLoans.setUserid(userid);
//        userLoans.setUserPaytime(new Date());


//        for(Integer i=0;i<Integer.parseInt(loans.getLoansMonth());i++){
//            UserMonloans userMonloans1=new UserMonloans();
//            userMonloans1.setUserid(userid);
//            userMonloans1.setLoansid(10);
//            userMonloans1.setRepayid(loans.getRepayid());
//            userMonloans1.setMonloansPaymonth(loans.getLoansMoney());
//            String futureDate = FutureDateUtils.getFutureDate(i * 30, new Date());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = sdf.parse(futureDate);
//            userMonloans1.setMonloansTime(date);
//            userMonloans1.setMonloansBalance(loans.getLoansMoney());
//
//            String monloansLastmonth=Long.toString(Integer.parseInt(loans.getLoansMonth())-i);
//            userMonloans1.setMonloansLastmonth(monloansLastmonth);
//            userMonloans1.setMonloansStatus("还未到期");
//            borrowService.addLoansMouth(userMonloans1);
//            System.out.println(userMonloans1);
//        }

        borrowService.addLoansAnduserZong(loans, userZong);

        return 10;


    }

    @RequestMapping("/addMonthLoans")
    public int addMonthLoans(Integer id,HttpServletRequest request) throws ParseException {
        Loans loans = borrowService.findLoansById(id);
        //UserLoans userLoans = borrowService.findUserLoans(id);

        for(Integer i=0;i<Integer.parseInt(loans.getLoansMonth());i++){
            UserMonloans userMonloans1=new UserMonloans();
            userMonloans1.setUserid(1);
            userMonloans1.setLoansid(loans.getLoansId());
            userMonloans1.setRepayid(loans.getRepayid());
            userMonloans1.setMonloansMonth(loans.getLoansMonth());
            userMonloans1.setMonloansPaymonth(loans.getLoansMoney()/Integer.parseInt(loans.getLoansMonth()));
            userMonloans1.setMonloansPaynext(loans.getLoansMoney()-(loans.getLoansMoney()/Integer.parseInt(loans.getLoansMonth())) * i);
            userMonloans1.setMonloansPaybefore(loans.getLoansMoney()-(loans.getLoansMoney()/Integer.parseInt(loans.getLoansMonth())) * i);
            userMonloans1.setMonloansPaynow(loans.getLoansMoney()/Integer.parseInt(loans.getLoansMonth()));

            String futureDate = FutureDateUtils.getFutureDate(i * 30,loans.getLoansTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(futureDate);
            userMonloans1.setMonloansTime(date);
            userMonloans1.setMonloansBalance(loans.getLoansMoney());
            String monloansLastmonth=Long.toString(Integer.parseInt(loans.getLoansMonth())-i);
            userMonloans1.setMonloansLastmonth(monloansLastmonth);
            userMonloans1.setMonloansStatus("还未到期");
            if(i==Integer.parseInt(loans.getLoansMonth())){
              //  userLoans.setUserStatus("已还款");
            }
           // borrowService.updateUserLoans(userLoans);
            borrowService.addLoansMouth(userMonloans1);

        }

        return 0;
    }

    @RequestMapping("/giveBrrow")
    public int giveBrrow(@RequestBody Map<String,String> map,HttpServletRequest request){
        System.out.println("==================="+map.get("loadId"));
        System.out.println("+++++++++++=="+map.get("money"));

        String token = request.getHeader("token");
        Integer userid = Integer.parseInt(token);
        User byUserId = borrowService.findByUserId(userid);
        int money = Integer.parseInt(map.get("money"));

        byUserId.setUserAmount(byUserId.getUserAmount()-money);
        if(byUserId.getUserAmount()<money){
           return 1;
        }
        int loadId = Integer.parseInt(map.get("loadId"));
        Loans loansById = borrowService.findLoansById(loadId);

        loansById.setLoansMoney(loansById.getLoansMoney()-money);

        if(loansById.getLoansMoney()<=0){
            loansById.setLoansState("1");
        }
        borrowService.updateLoans(loansById);

        borrowService.updateUser(byUserId);

        return 0;
    }







}
